﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using QLHREntities;
using QLHuongRungBUS;

namespace QLHuongRung
{
    public partial class NhapKho : Form
    {
        private List<DonDatHang> list_ddh = new List<DonDatHang>();

        private DonDatHangBUS ddhBUS = new DonDatHangBUS();
        private NguyenLieuBUS nlBUS = new NguyenLieuBUS();
        private bool isLoadingComplete = false;
        private int MaDDH_CapNhat = -1;
        public NhapKho()
        {
            InitializeComponent();
        }

        private void NhapKho_Load(object sender, EventArgs e)
        {
            SetCbbDonDatHang();

            isLoadingComplete = true;
        }

        private void SetCbbDonDatHang()
        {
            list_ddh = ddhBUS.LayDanhSachDonDatHang();

            if (list_ddh.Count > 0)
            {
                var dataDDH = new BindingList<KeyValuePair<int, string>>();

                foreach (var item in list_ddh)
                {
                    dataDDH.Add(new KeyValuePair<int, string>(item.MaDDH, item.MaDDH.ToString("0000") + " --- " + ((DateTime)item.NgayDatHang).ToString("dd/MM/yyyy")));
                }
                cbbDonDatHang.DataSource = null;
                cbbDonDatHang.DataSource = dataDDH;
                cbbDonDatHang.ValueMember = "Key";
                cbbDonDatHang.DisplayMember = "Value";
                cbbDonDatHang.SelectedIndex = 0;
            }
            else
            {
                var dataDDH = new BindingList<KeyValuePair<int, string>>();
                cbbDonDatHang.DataSource = dataDDH;
                cbbDonDatHang.Refresh();
                cbbDonDatHang.Update();
            }
                
        }

        private void cbbDonDatHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isLoadingComplete)
            {
                KeyValuePair<int, string> selectedEntry = (KeyValuePair<int, string>)((ComboBox)sender).SelectedItem;
                int maDDH = selectedEntry.Key;
                MaDDH_CapNhat = maDDH;
                List<CTDDH> list_ctddh = new List<CTDDH>();
                list_ctddh = ddhBUS.LayChiTietDonDatHang(maDDH);

                dGvChiTietDDH.DataSource = list_ctddh;
            }
        }

        private void btnNhapKho_Click(object sender, EventArgs e)
        {
            if (MaDDH_CapNhat != -1)
            {
                List<ChiTietThucDon> list_cttd = new List<ChiTietThucDon>();
                list_cttd = ddhBUS.LayChiTietThucDonByMaDDH(MaDDH_CapNhat);

                foreach (var item in list_cttd)
                {
                    NguyenLieu nl = nlBUS.LayNguyenLieu((int)item.NguyenLieu);
                    nl.SoLuongTon += item.SoLuongDat;
                    nlBUS.CapNhatKho(nl);
                }

                ddhBUS.CapNhatTinhTrangGiao(MaDDH_CapNhat);
                MessageBox.Show("Cập nhật thành công!!!");
                SetCbbDonDatHang();
                MaDDH_CapNhat = -1;
            }
            else MessageBox.Show("Hãy chọn đơn đặt hàng khác!!!");
        }
    }

    
}
